<?php
 
	function smarty_block_purchases($params, $content, &$smarty, &$repeat) {

		global $db, $config, $counter;

		$l = count($smarty->_tag_stack);
		
		
		if(empty($params['name'])) $params['name'] = "purchases";

		if(empty($smarty->blockvars[$l])) {
			$sqlstr = "select a.*,b.title as supplier_name from {$config->db_prefix}purchases_paper a left join {$config->db_prefix}suppliers b on a.supplierid=b.id where 1=1";
			if (!empty($params['shopid'])) $sqlstr .= " and a.shopid = ".$db->qstr($params['shopid']);
			if (!empty($params['paper_id'])) $sqlstr .= " and a.id = ".$db->qstr($params['paper_id']);
			if (!empty($params['paper_title'])) $sqlstr .= " and a.title like ".$db->qstr("%".$params['paper_title']."%");
			if (!empty($params['sdate'])) $sqlstr .= " and a.create_time>='".$params['sdate']."'";
			if (!empty($params['edate'])) $sqlstr .= " and a.create_time<='".$params['edate']." 23:59:59'";
			if (!empty($params['supplier'])) $sqlstr .= " and b.title like ". $db->qstr("%".$params['supplier']."%");
			
			
			if($params['status']==="0"){
			    $sqlstr .= " and a.status= 0";
			}elseif ($params['status']==="1"){
			    $sqlstr .= " and a.status> 0";
			}elseif ($params['status']==="-1"){
			    $sqlstr .= " and a.status= -1";
			}
			
			if (!empty($params['order'])) $sqlstr .= " order by {$params['order']}";

			$limit = empty($params['limit'])?9999:$params['limit'];
			$page = empty($params['page'])?1:$params['page'];
			$pager = new pager($limit, $page);
			
			//echo $sqlstr;
			$smarty->blockvars[$l] = $pager->execute($db, $sqlstr);

			//
			if(!$smarty->blockvars[$l]) return $repeat = false;
			if(!empty($params['pager'])) $smarty->assign($params['pager'], $pager);
		}
		if(list($key, $item) = each($smarty->blockvars[$l])) {
			$repeat = true;
			if(!empty($counter)) $item["counter"] = $counter;
			$smarty->assign($params['name'], $item);
		}
		else {
			$repeat = false;
			$counter = "";
			reset($smarty->blockvars[$l]);
		}
		
		if(!is_null($content)) print $content;
	
		//
		if(!$repeat) $smarty->blockvars[$l] = array();
	}

?>